package com.haide.channel.pro.service.impl;

import com.haide.channel.pro.constant.ConfigDict;
import com.haide.channel.pro.constant.NumberDict;
import com.haide.channel.pro.constant.NumberStrDict;
import com.haide.channel.pro.enums.ErrorCodeEnum;
import com.haide.channel.pro.exception.BizServiceException;
import com.haide.channel.pro.pojo.Result;
import com.haide.channel.pro.pojo.bo.QueryDownReqBo;
import com.haide.channel.pro.pojo.dto.BaseReqDto;
import com.haide.channel.pro.pojo.dto.QueryDownRespDto;
import com.haide.channel.pro.service.QueryDownService;
import com.haide.channel.pro.util.ExceptionUtils;
import com.haide.channel.pro.util.HttpUtil;
import com.haide.channel.pro.util.RequestUtil;
import com.haide.channel.pro.util.verifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 查询支持区域业务实现
 * <p>
 * 1.
 * </p>
 *
 * @author : wukong
 * @version : 1.0.0
 * @date : 2018/7/18
 */
@Slf4j
@Service
public class QueryDownServiceImpl implements QueryDownService {
    @Autowired
    private HttpUtil httpUtil;

    @Override
    public Result<Boolean> queryDown(QueryDownReqBo queryDownReqBo) {
        log.info("查询支持区域-请求", queryDownReqBo);
        Result<Boolean> result;
        try {
            BaseReqDto queryDownReqDto = new BaseReqDto();
            queryDownReqDto.setPhone(ConfigDict.PHONE);
            queryDownReqDto.setAgentNo(ConfigDict.AGENT_NO);
            String resultStr = httpUtil.get(RequestUtil.getUrl(queryDownReqDto, ConfigDict.QUERY_DOWN_URL));
            QueryDownRespDto queryDownRespDto = verifyUtil.getSuccessResult(resultStr, QueryDownRespDto.class);
            int flag = NumberDict.ZERO;
            String provinceId = NumberStrDict.MINUS_ONE;
            log.info("查询支持区域-判断省份信息");
            for (QueryDownRespDto.DataBean.ProvinceListBean item : queryDownRespDto.getData().getProvinceList()) {
                if (item.getValue().equals(queryDownReqBo.getProvinceName())) {
                    flag++;
                    provinceId = item.getId();
                    log.info("查询支持区域-省份信息{}", item);
                }
            }
            if (flag == NumberDict.ONE) {
                log.info("查询支持区域-判断城市信息");
                for (QueryDownRespDto.DataBean.CityListBean item : queryDownRespDto.getData().getCityList()) {
                    if (item.getValue().equals(queryDownReqBo.getCityName())) {
                        flag++;
                        log.info("查询支持区域-城市信息{}", item);
                        if (provinceId.equals(item.getParentId())) {
                            log.info("查询支持区域-省份和城市匹配");
                            flag++;
                        }
                    }
                }
            }
            judgeException(flag);
            result = new Result<>(Boolean.TRUE);
        } catch (Exception e) {
            log.error("查询支持区域-异常", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("查询支持区域，响应:{}", result);
        return result;
    }

    @Override
    public Result<QueryDownRespDto> queryDown() {
        Result<QueryDownRespDto> result;
        try {
            BaseReqDto queryDownReqDto = new BaseReqDto();
            queryDownReqDto.setPhone(ConfigDict.PHONE);
            queryDownReqDto.setAgentNo(ConfigDict.AGENT_NO);
            String resultStr = httpUtil.get(RequestUtil.getUrl(queryDownReqDto, ConfigDict.QUERY_DOWN_URL));
            QueryDownRespDto queryDownRespDto = verifyUtil.getSuccessResult(resultStr, QueryDownRespDto.class);
            result = new Result<>(queryDownRespDto);
        } catch (Exception e) {
            log.error("查询支持区域-异常", e);
            result = ExceptionUtils.getResponse(e);
        }
        log.info("查询支持区域，响应:{}", result);
        return result;
    }

    /**
     * 判断异常
     *
     * @param flag
     */
    private void judgeException(int flag) {
        switch (flag) {
            case NumberDict.ZERO:
                throw new BizServiceException(ErrorCodeEnum.ERROR_CODE_200002);
            case NumberDict.ONE:
                throw new BizServiceException(ErrorCodeEnum.ERROR_CODE_200003);
            case NumberDict.TWO:
                throw new BizServiceException(ErrorCodeEnum.ERROR_CODE_200004);
        }
    }

}
